package cn.xjt.hello;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @author xujiangtao
 * @create 2020-07-31-13:15
 */

/***
 * 主窗口
 */
public class WebDownload {
    public static void main(String[] args) {
        dThread d = new dThread("https://i0.hdslb.com/bfs/sycp/creative_img/202007/c522b6de0506edaa1098703aaf684a2e.jpg","宇航员.jpg");
        dThread d2 = new dThread("https://t9.baidu.com/it/u=583874135,70653437&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1596778007&t=6b01deba697e5f06943396100bd58738","动物.jpg");
        dThread d3=new dThread("https://t7.baidu.com/it/u=3616242789,1098670747&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1596778007&t=e11ba092f9d1dcc4199a869481fe3e4d","美女.jpg");
        d.setName("线程一");
        d2.setName("线程二");
        d3.setName("线程三");
        d.start();
        d2.start();
        d3.start();
    }
}

/**
 * 线程类
 */
class dThread extends Thread{
    private String url;
    private String name;
    public dThread(String url,String name)
    {
        this.url=url;
        this.name= name;
    }
    @Override
    public void run() {
        Download d = new Download();
        System.out.println(Thread.currentThread().getName()+"开始下载");
        d.imageDownload(url,name);
    }
}

/**
 * 下载器
 */
class Download{
    public void imageDownload(String url,String name)
        {
            try {
                FileUtils.copyURLToFile(new URL(url),new File(name));
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("IO异常了，下载方法出现问题");
            }
        }
}
